<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class DoctorCashController extends Controller
{
    public function echoJson($arr)
    {
        echo json_encode($arr);
    }

    /*
     帐户余额：
     币种---到商品表----查出商铺的名称-----根据商铺的名称查找出币种
      提现金额---使用了事务---医生表的余额 ---提现中的金额-----商户的余额 支付金额 -------商户日志表的相关信息

     */
    public function balance(){


        if (intval(I('request.id'))=='' || I('request.md5')=='' ) {

                $this->echoJson(array('result' => 1, 'info' => '参数错误'));exit;
        } else {

            $id = intval(I('request.id'));
            $md5= I('request.md5');
            $eq=md5(2016);
            if($md5!=$eq){
                $this->echoJson(array('result' => 1, 'info' => '错误操作'));exit;
            }else {
                $Doctor = D('Doctor');
                $Goods = D('Goods_base');
                $Shop = D('Shop_base');
                $result = array();
                $auth_status = $Doctor->where('id=' . $id)->getField('auth_status');
                if ($auth_status != 2) {
                    $this->echoJson(array('result' => 1, 'info' => '账号未认证'));exit;
                } else {

                    $doctor = $Doctor->where('id=' . $id)->field('id,goods_id,money,cash')->find();

                    if ($doctor) {
                        $result['id'] = $doctor['id'];
                        $result['money'] = $doctor['money'];
                        $result['cash'] = $doctor['cash'];

                        if ($doctor['goods_id'] == 0) {
                            $this->echoJson(array('result' => 1, 'info' => '没有对应的商户不能提现'));exit;
                        } else {
                            $goods_id = $doctor['goods_id'];
                            $shop_id = $Goods->where('id=' . $goods_id)->getField('shop_id');
                            if ($shop_id) {
                                $shopMap['id']=$shop_id;
                                $shopInfo= $Shop->where($shopMap)->field('bz,u_poundage')->find();
                                if ($shopInfo) {
                                    $result['bz'] = $shopInfo['bz'];
                                    $result['u_poundage']=$shopInfo['u_poundage'];
                                }
                            }
                        }
                    }
                    if ($result) {
                        $this->echoJson(array('result' => 0, 'info' => $result));exit;
                    } else {
                        $this->echoJson(array('result' => '未知错误'));exit;
                    }
                }
            }
        }
    }

    /*
        写取出多少钱，
        //提交过来的，余额对比，我再一次对比。
        要把balance数据拿过来匹配  和根据 id 查出来的金额进行对比，如果对比结果为True的话，执行下一个操作，

     */
    public function withdrawals()
    {
        if (intval(I('request.number'))!='' && intval(I('request.id'))!='' && I('request.md5')!='') {
            $number = intval(I('request.number'));
            $id = intval(I('request.id'));

            $md5= I('request.md5');
            $eq=md5(2016);
            if($md5!=$eq) {
                $this->echoJson(array('result' => 1, 'info' => '错误操作'));exit;
            }
            //　强制转成bool浮点类型
            $input_money = (bool)(I('request.money'));
            $input_cash = (bool)(I('request.cash'));
            $Doctor = D('Doctor');
            if ($number < 2000) {//提现金额书写错误时
                $this->echoJson(array('result' => 1, 'info' => '最低提现金额为2000'));exit;
            } else {// id ,auth_status 是否合法
                $map['id'] = $id;
                $map['auth_status'] = 2;
                $info = $Doctor->where($map)->field('goods_id,cash,money')->find();//商品的id,余额，提现金额

                if (!$info) {
                    $this->echoJson(array('result' => 1, 'info' => '非法访问'));exit;
                } else {// 提交过来的数据和 数据库里请求的数据 进行对比
                    $money = $info['money'];
                    $cash = $info['cash'];
                    $goods_id = $info['goods_id'];

                    if (($input_money != $money)|| ($input_cash != $cash)){
                        var_dump($input_money);
                        $this->echoJson(array('result' => 1, 'info' => '非法访问'));exit;
                    } else {
                        //余额不足的情况下
                        if (!($money - $number)) {
                            $this->echoJson(array('result' => 1, 'info' => '余额不足'));exit;
                        }else{
                        //===================获取商铺==============================
                            $Goods = D('goods_base');
                            $goodsMap['id']=$goods_id;
                            $goodsMap['class_id']=3;
                            $GoodsInfo = $Goods->where($goodsMap)->field('shop_id')->find();//取得商店id,手续费
                            $shop_id=$GoodsInfo['shop_id'];

                            //============================================================
                            $Shop=D('Shop_base');
                            $poundage=$Shop->where('id='.$shop_id)->getField('u_poundage');//手续费
                            $Manager = D('Manager');
                            $managerInfo=$Manager->where('shop_id='.$shop_id)->field('id,username,money')->find();
                            if($managerInfo['money']<$number){
                                $this->echoJson(array('result' => 1, 'info' => '商户余额不足'));exit;
                            }
                            $managerId=$managerInfo['id'];
                            $managerName=$managerInfo['username'];
                            //=====================执行事务=================================

                            $Doctor->startTrans();
                            $DoctorMoney['money'] = $money - $number;
                            $DoctorMoney['cash'] = $cash + $number;
                            $doctorResult = $Doctor->where('id=' . $id)->save($DoctorMoney);
                      //=======================================================================
                            $newMoney=$number*$poundage;//手续费
                            $percentage=$number-$newMoney;//余额
                            $time=date('Y-m-d H:i:s');
                            $log['manager_id']=$managerId;
                            $log['doctor_id']=$id;
                            $log['money']=$number;
                            $log['log']="名为{$managerName}的商户于{$time}执行了提现操作，提现金额为{$number}手续费为{$newMoney}到账金额为{$percentage}";
                            $log['action']=1;
                            $log['create_time']=time();
                            $log['s_money']=$newMoney;
                            $ManagerLog= D('Manager_money_log');
                            $logResult = $ManagerLog->add($log);
                            if($doctorResult  && $logResult){
                                $Doctor->commit();
                                $this->echoJson(array('result'=>0,'info'=>'提现成功'));exit;
                            }else{
                                $Doctor->rollback();
                                $this->echoJson(array('result'=>1,'info'=>'提现失败'));exit;
                            }
                        }
                    }
                }
            }
        } else {
            $this->echoJson(array('result' => 1, 'info' => '参数错误'));exit;
        }
    }


    //==============================================
    /*
            type=1;提现中
            type=2;已经提现
    */
      public function extraction()
      {
          if (I('request.id')=='' || I('request.type') =='') {
              $this->echoJson(array('result' => 1, 'info' => '参数错误'));exit;
          } else {
              $type=intval(I('request.type'));
              $id = intval(I('request.id'));
              $Manager_money_log = D('Manager_money_log');
              $log = $Manager_money_log->where('doctor_id=' . $id)->select();
              if (!$log) {//如果交易记录不存在的情况
                  $this->echoJson(array('result' => 1, 'info' => '没有相关记录'));exit;
              } else {//goods_id
                  $Doctor = D('Doctor');
                  $goods_id = $Doctor->where('id=' . $id)->getField('goods_id');
                  if ($goods_id) {//获得商品的商户
                      $Goods = D('Goods_base');
                      $shop_id = $Goods->where('id=' . $goods_id)->getField('shop_id');
                      if ($shop_id) {//获得汇率和手续费
                          $Shop = D('Shop_base');
                          $shopInfo = $Shop->where('id='.$shop_id)->field('bz,u_poundage')->find();
                          if ($shopInfo) {
                              $bz = $shopInfo['bz'];
                              $pay_percentage = $shopInfo['u_poundage'];
                               //提现中
                              if($type==1){
                                  $moneyMap['action']=1;
                                  $moneyMap['doctor_id']=$id;
                                  $money=$Manager_money_log->where($moneyMap)->field('money,create_time,s_money')->order('create_time desc')->select();
                                   if($money){
                                       foreach($money as $key=>&$value){
                                           $value['bz']=$bz;
                                           $value['u_poundage']=$pay_percentage;
                                           $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                                           $value['s_money']=$value['money']-$value['s_money'];
                                       }

                                       $this->echoJson(array('result'=>0,'info'=>$money));exit;
                                   }
                              }
                              if($type==2){
                                  $actionMap['action']=2;
                                  $actionMap['doctor_id']=$id;
                                  $money=$Manager_money_log->where($actionMap)->field('money,update_time,s_money')->order('update_time desc')->select();
                                  if($money){
                                      foreach($money as $key=>&$value){
                                          $value['bz']=$bz;
                                          $value['u_poundage']=$pay_percentage;
                                          $value['update_time']=date('Y-m-d H:i:s',$value['update_time']);
                                          $value['s_money']=$value['money']-$value['s_money'];
                                      }

                                      $this->echoJson(array('result'=>0,'info'=>$money));exit;
                                  }


                              }
                          }
                      }
                  }
              }
          }

      }

    public function income(){
        if(I('request.id') ==""){
            $this->echoJson(array('result'=>1,'info'=>'参数错误'));exit;
        }else{
            $id=I("request.id");
            $Order=D('Order_info');
            $map['doctor_id']=$id;
            $map['order_status']=1;
            $map['lid']=3;
            $orderInfo=$Order->where($map)->field('order_no,goods_total_price,create_time')->order('create_time desc')->select();

            if(!$orderInfo){



                $this->echoJson(array('result'=>1,'info'=>"没有相关记录"));exit;
            }
            foreach($orderInfo as $key=>&$value){
                $value['create_time']=date('Y年m月d日H:i:s',$value['create_time']);

            }
            $this->echoJson(array('result'=>0,'info'=>$orderInfo));
        }

    }

}

